import asyncio
import time

# 定义一个异步函数（协程）
async def say_hello(name, delay):
    print(f"Start saying hello to {name}")
    await asyncio.sleep(delay)  # 模拟一个耗时操作
    print(f"Hello, {name}!")

# 定义一个异步函数（协程），用于并发执行多个任务
async def main():
    # 创建多个协程对象
    # task1 = asyncio.create_task(say_hello("Alice", 2))
    # task2 = asyncio.create_task(say_hello("Bob", 1))
    # task3 = asyncio.create_task(say_hello("Charlie", 3))
    #
    # # 等待所有任务完成
    # await task1
    # await task2
    # await task3
    tasks = [asyncio.create_task(say_hello("gary"+str(i), i)) for i in range(5)]
    await asyncio.gather(*tasks)

# 运行主协程
if __name__ == "__main__":
    start_time = time.time()
    asyncio.run(main())
    end_time = time.time()
    print(f"Total time taken: {end_time - start_time} seconds")
